<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : common.h.php / Development Code Evolve
	 *	Created / Modify : 2010-2-18 / --
	 */

	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	//	[CH]	生成随机的Ajax Hash字符串
	function getAjaxHash ()
	{
		$hash	=	array();
		$hash['reg']	=	getMeHash( 'register' );		//	0
		$hash['email']	=	getMeHash( 'email' );			//	12
		$hash['username']	=	getMeHash( 'username' );	//	24
		$hash['authcode']	=	getMeHash( 'authcode' );	//	36
		$hash['doRegister']	=	getMeHash( 'do-register' );	//	48
		$hash['doLogin']	=	getMeHash( 'do-login' );	//	60
		$hash['loginout']	=	getMeHash( 'loginout' );	//	72
		$hash['saveposts']	=	getMeHash( 'saveposts' );	//	84
		$hash['submitpost']	=	getMeHash( 'submitposts' );	//	96

		return	implode( '', $hash );
	}

	//	[CH]	判断用户是否已经登录
	function isUserLogin ()
	{
		global	$db;

		//	[CH]	如果开启强制验证模式,就把用户登录状态设置为false
		if( ME_FORCE_AUTH && isset( $_SESSION['auth'] ) )
			$_SESSION['auth']	=	false;
		else
			$_SESSION['auth']	=	isset( $_SESSION['auth'] ) ? $_SESSION['auth'] : false;

		//	[CH]	如果用户登录状态为false
		if( ME_FORCE_AUTH && !$_SESSION['auth']  && isset( $_SESSION['username'] ) && isset( $_SESSION['password'] ) && isset( $_SESSION['UID'] ) && isset( $_SESSION['email'] ) )
		{
			if( $db->MC->get( session_id() ) )
			{
				$result	=	$db->MC->get( session_id() );
				return	$_SESSION['auth']	=	$_SESSION['UID'] == $result['UID'] && $_SESSION['username'] == $result['username'] && $_SESSION['password'] == $result['password'] && $_SESSION['email'] == $result['email'] ? TRUE : false;
			}
			else
			{
				$result	=	$db->query( "SELECT `uid` FROM `mdk_users` WHERE `username` = '{$_SESSION['username']}' AND `uid` = '{$_SESSION['UID']}' AND `password` = '{$_SESSION['password']}' AND `email` = '{$_SESSION['email']}'" );
				$db->MC->add( session_id(), $_SESSION );
				return	$_SESSION['auth']	=	$result ? TRUE : false;
			}
		}

		return;//	$_SESSION['auth'];
	}

	//	[CH]	检测Email地址是否合法
	function isEmail ( $value )
	{
		if( preg_match( "/^[\w\.]+@([A-Z_a-z0-9-]+\.)+[a-z0-9A-Z]{2,4}$/i", $value ) )
			return	true;
		else
			return	false;
	}

	function isNumber ( $value )
	{
		if( is_numeric( $value ) && strlen( $value ) == 5 )
			return	true;
		else
			return	false;
	}

	//	[CH]	判断数据库中的数据是否存在
	function dataExists ( $data, $value, $table, $type = false )
	{
		/**
		 *	@parma $data	要查询的数据
		 *	@parma $value	$data 数据所对应的值
		 *	@parma $table	$table 要查询的表
		 *	@parma $type	要查询的字段, 默认为 `uid`
		 */
		global	$db;
		$type	=	$type ? $type : 'uid';
		$result	=	$db->assoc( $db->query( "SELECT `{$type}` FROM `{$table}` WHERE `{$data}` = '{$value}'" ) );

		return	$result[$type] ? 'Err[1]' : 'Err[0]';
	}

	//	[CH]	检测用户名是否合法
	function isUsername ( $value )
	{
		if( preg_match( "/^[\w\.]{4,20}$/i", $value ) )
			return	true;
		else
			return	false;
	}

	//	[CH]	字符串截取函数
	function MDKsubstr ( $string, $length = false )
	{
		$MDKStringLength	=	$length ? $length : ME_DEFAULT_STRING_LENGTH;
		$suffix				=	strlen( $string ) > $MDKStringLength ? '...' : '';

		//	[CH]	初始化中英文字符数
		$temp1	=	$temp2	=	0;

		for ( $i = 0; $i <= $MDKStringLength; $i++)
		{
			if ( ord( substr( $string, $i, 1 ) ) >= 128 )
				$temp1++;		//	[CH]	统计中文字符数
			else
				$temp2++;		//	[CH]	统计英文字符数
		}

		if ( $temp1 % 3 == 1 )
			$temp1	=	$temp1 + 2;
		elseif ( $temp2 % 3 == 2 )
			$temp1	=	$temp1 + 1;

		return substr( $string, 0, $temp1 + $temp2 ) . $suffix;
	}

	//	[CH]	论坛导航链接
	function getForumName ( $fid = false )
	{
		global	$MDK, $forumsResult;

		if( $MDK['do'] == 'forum' )
		{
			if( !isset( $MDK['f'] ) )
				return	'&nbsp;';
			else
				$fid	=	$MDK['f'];
		}

		$forumName	=	'';
		$forumArr	=	array();

		$currentForum	=	$forumsResult[intval( $fid - 1 )];

		if( $currentForum['firstParentId'] != 0 )
		{
			$forumArr	=	$forumsResult[intval( $currentForum['firstParentId'] - 1 )];
			$forumName	=	" &rsaquo; <a href=\"./?f={$forumArr['fid']}\">{$forumArr['forumName']}</a>";

			if( $currentForum['secondParentId'] != 0 )
			{
				$forumArr	=	$forumsResult[intval( $currentForum['secondParentId'] - 1 )];
				$forumName	.=	" &rsaquo; <a href=\"./?f={$forumArr['fid']}\">{$forumArr['forumName']}</a>";

				if( $currentForum['thirdParentId'] != 0 )
				{
					$forumArr	=	$forumsResult[intval( $currentForum['thirdParentId'] - 1 )];
					$forumName	.=	" &rsaquo; <a href=\"./?f={$forumArr['fid']}\">{$forumArr['forumName']}</a>";
				}
			}
		}

		return	$forumName	.=	$MDK['do'] == 'forum' && isset( $MDK['f'] ) && $currentForum['fid'] == $MDK['f'] ? " &rsaquo; {$currentForum['forumName']}" : " &rsaquo; <a href=\"./?f={$currentForum['fid']}\">{$currentForum['forumName']}</a>";
	}

	//	[CH]	获取斑竹或新用户信息
	function	getForumAdminInfo ( $forumAdmin, $fid )
	{
		$tempArr	=	$forumAdminForumArr	=	array();
		$string		=	'<span id="forum_admin">斑竹</span>&nbsp;';
		foreach( $forumAdmin as $tempArr )
		{
			if( $tempArr['fid'] == $fid )
				$forumAdminForumArr[]	=	$tempArr;
		}

		if( count( $forumAdminForumArr ) == 0 )
			return	$string	.=	"&nbsp;<a href=\"./?do=apply&f={$fid}\" title=\"我来做斑竹\">斑竹暂缺</a>";
		/**
		 *	这里预留了一个变量名
		 *	apply 用来申请斑竹
		 */

		foreach( $forumAdminForumArr as $info )
		{
			$string	.=	"&nbsp;<a href=\"./?do=user&u={$info['uid']}\">{$info['author']}</a>&nbsp;";
		}
		return	$string;
	}

	//	[CH]	获取文件后缀名
	function	getFileExtName ( $str )
	{
		$string	=	null;
		$point		=	strrpos( $str, "." );
		if( $point )
			$string	=	substr( $str, $point + 1/*, strlen( $str ) - $point*/ );
		return	$string;
	}

	//	[CH]	返回文件上传装态
	function	fileUploadStatus ( $id, $msg )
	{
		return	'<html>
				<head>
				<meta content="text/html; charset=utf-8" http-equiv=Content-type>
				</head>
				<body>
				<script>
					var parDoc = window.parent.document.getElementById;
					try{ parent.document.getElementById( "upload_status_' . $id . '" ).innerHTML	=	\'' . $msg . '\' } catch(e){};
				</script>
				</html>';
	}
?>